$(function () {
    //数据表格
    var btn_datagrid = $("#btn_datagrid");
    btn_datagrid.datagrid({
        url:"/studentShift/list.do",//从远程站点请求数据
        fit:true,//面板大小将自适应父容器
        fitColumns:true,//自动展开/收缩列的大小
        striped:true,//显示斑马线效果
        pagination:true,//显示分页工具栏
        columns:[[
            {field:"id",checkbox:true},//复选框
            {field:"name",title:"学生姓名",width:80},
            {field:"shiftTime",title:"转班时间",width:80},
            {field:"tel",title:"学生电话",width:80},
            {field:"grade",title:"之前的班级",width:80,formatter:function (value) {
                    return value ? value.className : "";
                }},
            {field:"newClassName",title:"转入的班级",width:80},
            {field:"salesman",title:"对应客服",width:80,formatter:function (value) {
                    return value ? value.username : "";
                }},
            {field:"shiftAudit",title:"转班状态",width:80,formatter:function (value) {
                    return value ? "已审核" : "未审核";
                }},
            {field:"money",title:"已缴学费",width:80},
            {field:"owe",title:"待缴学费",width:80}
        ]],
        toolbar:"#buttAll",
    })

    //弹框
    btn_dialog = $("#btn_dialog");
    btn_dialog.dialog({
        width:330,
        height:380,
        buttons:"#btn_buttons",
        openAnimation:'show',
        closed:true,
        onClose:function () {
            myForm.form('clear');
        }
    })


    //弹框表单
    myForm = $("#myForm");
    myForm.form({

    })

    //编辑下拉框数据
    //转入班级
    $("#grade").combobox({
        url:'/grade/list.do',//远程加载所有班级
        valueField:'className',//数据值名称绑定到该下拉列表框
        textField:'className'//数据字段名称绑定到该下拉列表框
    })






    //高级查询框
    gradeId = $("#gradeId");
    minTime = $("#minTime");
    maxTime = $("#maxTime");
    keyword = $("#keyword");
    gradeId.combobox({
        url:'/grade/list.do',//远程加载所有班级
        valueField:'id',//数据值名称绑定到该下拉列表框
        textField:'className'//数据字段名称绑定到该下拉列表框
    })


    //所有方法
    methods = {
        edit:function () {
            row = btn_datagrid.datagrid('getSelected');
            if (row == null) {
                $.messager.alert("温馨提示","至少需要选择一条数据","error");
                return;
            }
            row["newClassName"] = row.newClassName;
            row["grade.className"] = row.grade.className;
            myForm.form('load',row);
            btn_dialog.dialog('open');
            btn_dialog.dialog('setTitle',"修改数据");
        },
        audit:function () {
            row = btn_datagrid.datagrid('getSelected');
            if (row == null) {
                $.messager.alert("温馨提示","至少需要选择一条数据","error");
                return;
            }
            $.messager.confirm("温馨提示","确认审核",function (r) {
                if (r) {
                    $.get('/studentShift/audit.do?id='+row.id,function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","审核成功");
                            btn_datagrid.datagrid('reload');
                        }else {
                            $.messager.alert('温馨提示',data.msg,"error");
                        }
                    },"json");
                }
            })
        },

        save:function () {
            myForm.form('submit',{
                url:'/studentShift/update.do',
                success: function(){
                    $.messager.alert('温馨提示','保存成功');
                    methods["closes"]();
                    btn_datagrid.datagrid('reload');
                }
            })
        },
        closes:function () {
            myForm.form('clear');
            btn_dialog.dialog('close');
        },
        query:function () {
            gradeIdValue = gradeId.combobox('getValue');
            minTimeValue = minTime.datebox('getText');
            maxTimeValue = maxTime.datebox('getText');
            keywordValue = keyword.textbox('getText');
            btn_datagrid.datagrid('load',{
                url:'/studentShift/list.do',
                gradeId:gradeIdValue,
                minTime:minTimeValue,
                maxTime:maxTimeValue,
                keyword:keywordValue
            })
        }
    }

    //所有a标签点击事件触发的事件
    $("a[data-operation]").click(function () {
        met = $(this).data("operation");
        methods[met]();
    })


})